<template>
	<el-main class="bg edit_wrap">
		<el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">
		<el-row class="row_ce">
							<el-col v-if="$check_field('get','merchant_users') || $check_field('add','merchant_users') || $check_field('set','merchant_users')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="商家用户" prop="merchant_users">
													<el-select v-if="(form['catering_orders_id'] && $check_field('set','merchant_users')) || (!form['catering_orders_id'] && $check_field('add','merchant_users'))" id="merchant_users" v-model="form['merchant_users']" :disabled="disabledObj['merchant_users_isDisabled']">
							<el-option v-for="o in list_user_merchant_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
						<el-select v-else-if="$check_field('get','merchant_users')" id="merchant_users" v-model="form['merchant_users']" :disabled="true">
							<el-option v-for="o in list_user_merchant_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','merchant_name') || $check_field('add','merchant_name') || $check_field('set','merchant_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="商家姓名" prop="merchant_name">
												<el-input id="merchant_name" v-model="form['merchant_name']" placeholder="请输入商家姓名"
							  v-if="(form['catering_orders_id'] && $check_field('set','merchant_name')) || (!form['catering_orders_id'] && $check_field('add','merchant_name'))" :disabled="disabledObj['merchant_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','merchant_name')">{{form['merchant_name']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','name_of_homestay') || $check_field('add','name_of_homestay') || $check_field('set','name_of_homestay')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="民宿名称" prop="name_of_homestay">
												<el-input id="name_of_homestay" v-model="form['name_of_homestay']" placeholder="请输入民宿名称"
							  v-if="(form['catering_orders_id'] && $check_field('set','name_of_homestay')) || (!form['catering_orders_id'] && $check_field('add','name_of_homestay'))" :disabled="disabledObj['name_of_homestay_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','name_of_homestay')">{{form['name_of_homestay']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','food_name') || $check_field('add','food_name') || $check_field('set','food_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="美食名称" prop="food_name">
												<el-input id="food_name" v-model="form['food_name']" placeholder="请输入美食名称"
							  v-if="(form['catering_orders_id'] && $check_field('set','food_name')) || (!form['catering_orders_id'] && $check_field('add','food_name'))" :disabled="disabledObj['food_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','food_name')">{{form['food_name']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','food_prices') || $check_field('add','food_prices') || $check_field('set','food_prices')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="美食价格" prop="food_prices">
								<el-input-number id="food_prices" v-model.number="form['food_prices']"
						v-if="(form['catering_orders_id'] && $check_field('set','food_prices')) || (!form['catering_orders_id'] && $check_field('add','food_prices'))" :disabled="disabledObj['food_prices_isDisabled']"></el-input-number>
					<div v-else-if="$check_field('get','food_prices')">{{form['food_prices']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','ordinary_users') || $check_field('add','ordinary_users') || $check_field('set','ordinary_users')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="普通用户" prop="ordinary_users">
																		<div v-if="user_group !== '管理员'">
							{{ get_user_session_ordinary_users(form['ordinary_users']) }}
							<el-select v-if="(form['catering_orders_id'] && $check_field('set','ordinary_users')) || (!form['catering_orders_id'] && $check_field('add','ordinary_users'))" id="ordinary_users" v-model="form['ordinary_users']" :disabled="disabledObj['ordinary_users_isDisabled']">
								<el-option v-for="o in list_user_ordinary_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
							<el-select v-else-if="$check_field('get','ordinary_users')" id="ordinary_users" v-model="form['ordinary_users']" :disabled="true">
								<el-option v-for="o in list_user_ordinary_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
						</div>
						<el-select v-else id="ordinary_users" v-model="form['ordinary_users']" :disabled="disabledObj['ordinary_users_isDisabled']">
							<el-option v-for="o in list_user_ordinary_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
																</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','user_name') || $check_field('add','user_name') || $check_field('set','user_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="用户姓名" prop="user_name">
												<el-input id="user_name" v-model="form['user_name']" placeholder="请输入用户姓名"
							  v-if="(form['catering_orders_id'] && $check_field('set','user_name')) || (!form['catering_orders_id'] && $check_field('add','user_name'))" :disabled="disabledObj['user_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','user_name')">{{form['user_name']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','purchase_quantity') || $check_field('add','purchase_quantity') || $check_field('set','purchase_quantity')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="购买数量" prop="purchase_quantity">
								<el-input-number id="purchase_quantity" v-model.number="form['purchase_quantity']"
						v-if="(form['catering_orders_id'] && $check_field('set','purchase_quantity')) || (!form['catering_orders_id'] && $check_field('add','purchase_quantity'))" :disabled="disabledObj['purchase_quantity_isDisabled']"></el-input-number>
					<div v-else-if="$check_field('get','purchase_quantity')">{{form['purchase_quantity']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','total_purchase_price') || $check_field('add','total_purchase_price') || $check_field('set','total_purchase_price')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="购买总价" prop="total_purchase_price">
								{{toFixed(form['food_prices'] * form['purchase_quantity'])}}
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','room_location') || $check_field('add','room_location') || $check_field('set','room_location')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="房间位置" prop="room_location">
												<el-input id="room_location" v-model="form['room_location']" placeholder="请输入房间位置"
							  v-if="(form['catering_orders_id'] && $check_field('set','room_location')) || (!form['catering_orders_id'] && $check_field('add','room_location'))" :disabled="disabledObj['room_location_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','room_location')">{{form['room_location']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','a_room_number') || $check_field('add','a_room_number') || $check_field('set','a_room_number')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="房间号码" prop="a_room_number">
												<el-input id="a_room_number" v-model="form['a_room_number']" placeholder="请输入房间号码"
							  v-if="(form['catering_orders_id'] && $check_field('set','a_room_number')) || (!form['catering_orders_id'] && $check_field('add','a_room_number'))" :disabled="disabledObj['a_room_number_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','a_room_number')">{{form['a_room_number']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','memo') || $check_field('add','memo') || $check_field('set','memo')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="备注信息" prop="memo">
								<el-input type="textarea" id="memo" v-model="form['memo']" placeholder="请输入备注信息"
						v-if="(form['catering_orders_id'] && $check_field('set','memo')) || (!form['catering_orders_id'] && $check_field('add','memo'))" :disabled="disabledObj['memo_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','memo')">{{form['memo']}}</div>
							</el-form-item>
			</el-col>
						
	
	
		
		
	
	
	
	
	</el-row>
			<el-col :xs="24" :sm="12" :lg="8" class="el_form_btn_warp">
				<el-form-item v-if="$check_action('/catering_orders/view','set') || $check_action('/catering_orders/view','add') || $check_option('/catering_orders/table','examine')">
					<el-button type="primary" @click="submit()">提交</el-button>
					<el-button @click="cancel()">取消</el-button>
				</el-form-item>
				<el-form-item v-else>
					<el-button @click="cancel()">返回</el-button>
				</el-form-item>
			</el-col>

		</el-form>
	</el-main>
</template>

<script>
	import mixin from "@/mixins/page.js";

	export default {
		mixins: [mixin],
		data() {
			return {
				field: "catering_orders_id",
				url_add: "~/api/catering_orders/add?",
				url_set: "~/api/catering_orders/set?",
				url_get_obj: "~/api/catering_orders/get_obj?",
				url_upload: "~/api/catering_orders/upload?",

				query: {
					"catering_orders_id": 0,
				},

				form: {
								"merchant_users": 0, // 商家用户
										"merchant_name":  '', // 商家姓名
										"name_of_homestay":  '', // 民宿名称
										"food_name":  '', // 美食名称
										"food_prices":  0, // 美食价格
										"ordinary_users": 0, // 普通用户
										"user_name":  '', // 用户姓名
										"purchase_quantity":  0, // 购买数量
															"room_location":  '', // 房间位置
										"a_room_number":  '', // 房间号码
										"memo":  '', // 备注信息
											"catering_orders_id": 0, // ID
													},
				disabledObj:{
								"merchant_users_isDisabled": false,
										"merchant_name_isDisabled": false,
										"name_of_homestay_isDisabled": false,
										"food_name_isDisabled": false,
					          			"food_prices_isDisabled": false,
										"ordinary_users_isDisabled": false,
										"user_name_isDisabled": false,
					          			"purchase_quantity_isDisabled": false,
					          			"total_purchase_price_isDisabled": false,
										"room_location_isDisabled": false,
										"a_room_number_isDisabled": false,
										"memo_isDisabled": false,
										},

	
					// 用户列表
				list_user_merchant_users: [],
						
				
				
				
				
					// 用户列表
				list_user_ordinary_users: [],
						// 用户组
				group_user_ordinary_users: "",
						
				
				
				
				
				
			
			}
		},
		methods: {

	
	
				/**
			 * 获取商家用户用户列表
			 */
			async get_list_user_merchant_users() {
                var json = await this.$get("~/api/user/get_list?user_group=商家用户");
                if(json.result && json.result.list){
                    this.list_user_merchant_users = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
					get_user_merchant_users(id){
				var obj = this.list_user_merchant_users.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},
			
	
			
	
			
	
			
	
			
	
				/**
			 * 获取普通用户用户列表
			 */
			async get_list_user_ordinary_users() {
                var json = await this.$get("~/api/user/get_list?user_group=普通用户");
                if(json.result && json.result.list){
                    this.list_user_ordinary_users = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
					/**
			 * 获取普通用户用户组
			 */
			async get_group_user_ordinary_users() {
							this.form["ordinary_users"] = this.$store.state.user.user_id;
							var json = await this.$get("~/api/user_group/get_obj?name=普通用户");
				if(json.result && json.result.obj){
					this.group_user_ordinary_users = json.result.obj;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
			get_user_session_ordinary_users(id){
				var _this = this;
				var user_id = {"user_id":id}
				var url = "~/api/"+_this.group_user_ordinary_users.source_table+"/get_obj?"
				this.$get(url, user_id, function(res) {
					if (res.result && res.result.obj) {
						var arr = []
						for (let key in res.result.obj) {
							arr.push(key)
						}
						var arrForm = []
									for (let key in _this.form) {
							arrForm.push(key)
						}
												_this.form["ordinary_users"] = id
									_this.disabledObj['ordinary_users' + '_isDisabled'] = true
						for (var i=0;i<arr.length;i++){
						  if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
							for (var j = 0; j < arrForm.length; j++) {
							  if (arr[i] === arrForm[j]) {
								if (arr[i] !== "ordinary_users") {
			                      _this.form[arrForm[j]] = res.result.obj[arr[i]]
			                      _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								  break;
								} else {
								  _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								}
							  }
							}
						  }
						}
					}
				});
			},
					get_user_ordinary_users(id){
				var obj = this.list_user_ordinary_users.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},
			
	
			
	
			
	
										set_total_purchase_price(){
				this.form.total_purchase_price = parseFloat(this.form.food_prices) * parseFloat(this.form.purchase_quantity)
			},
							// 自动计算精度处理
		toFixed: function(num){
			if (!isNaN(num)) {
				return ((num + '').indexOf('.') == -1) ? num : num.toFixed(2);
			}
		},
		
	
			
	
			
	
		
			/**
			 * 获取对象之前
			 * @param {Object} param
			 */
			get_obj_before(param) {
				var form = "";
																														// 获取缓存数据附加
				form = $.db.get("form");
									$.push(this.form ,form);
												if(this.form && form){
					Object.keys(this.form).forEach(key => {
						Object.keys(form).forEach(dbKey => {
							// if(dbKey === "charging_standard"){
							// 	this.form['charging_rules'] = form[dbKey];
							// 	this.disabledObj['charging_rules_isDisabled'] = true;
							// };
							if(key === dbKey){
								this.disabledObj[key+'_isDisabled'] = true;
								this.form[key] = form[dbKey]
							}
							if(dbKey === "source_table"){
								this.form['source_table'] = form[dbKey];
							}
							if(dbKey === "source_id"){
								this.form['source_id'] = form[dbKey];
							}
							if(dbKey === "source_user_id"){
								this.form['source_user_id'] = form[dbKey];
							}
						})
					})
				}
																												$.db.del("form");
				return param;
			},

			/**
			 * 获取对象之后
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json, func){

																																																												

			},

																																																																				/**
					 * 提交前事件
					 * @param {Object} param 提交参数
					 */
					submit_before: function(param) {
						// console.log('提交前事件', param);
						// 保存计算结果
																																																																																																																													this.set_total_purchase_price()
																																																										param = this.form
						// 循环参数
						for(var key in param){
							// 错误回调
							try{
								var value = param[key];
								// 值判断与值校验
								console.log("键 ,值 ,类型" ,key ,value ,value instanceof Date);
								if(value && value instanceof Date){
									param[key] = this.$toTime(value ,"yyyy-MM-dd hh:mm:ss");
								}
							}
							catch(err){
								console.log(key ,"转日期错误：" ,err ,"收到请无视!");
							}
						}
						return param;
					},
								async submit(param, func){
				if (!param) {
					param = this.form;
				}
								var pm = this.events("submit_before", Object.assign({}, param)) || param;
				var msg = await this.events("submit_check", pm);
				var ret;
				if (msg) {
					this.$toast(msg, 'danger');
				} else {
																																																																																												ret = this.events("submit_main", pm, func);
				}
				return ret;
			},
			/**
			 * 提交前验证事件
			 * @param {Object} 请求参数
			 * @return {String} 验证成功返回null, 失败返回错误提示
			 */
						submit_check(param) {
																																																																																																																																														return null;
			},

			is_view(){
				// var bl = this.user_group == "管理员";
				var bl = false;

				if(!bl){
					bl = this.$check_action('/catering_orders/table','add');
					console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
				}
				if(!bl){
					bl = this.$check_action('/catering_orders/table','set');
					console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
				}
				if(!bl){
					bl = this.$check_action('/catering_orders/view','add');
					console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
				}
				if(!bl){
					bl = this.$check_action('/catering_orders/view','set');
					console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
				}
				if(!bl){
					bl = this.$check_action('/catering_orders/view','get');
					console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
				}

				console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

				return bl;
			},
			/**
			 * 上传文件
			 * @param {Object} param
			 */
			uploadimg(param) {
				this.uploadFile(param.file, "avatar");
			},

		},
		created() {
					this.get_list_user_merchant_users();
																this.get_list_user_ordinary_users();
					this.get_group_user_ordinary_users();
																	},
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
	
	.img_multiple{
		overflow: hidden;
	}
	.img_multiple .img_block{
		float: left;
		margin-right: 5px;
		margin-bottom: 5px;
		position: relative;
	}
	.img_multiple .img_block img{
		height: 100px;
		width: auto;
	}
	.img_multiple .img_del{
		position: absolute;
		top: 5px;
		right: 5px;
		width: 20px;
		height: 20px;
		background: #0000008a;
		color: #fff;
		line-height: 20px;
		text-align: center;
		border-radius: 100%;
		cursor: pointer;
	}




</style>
